package com.sunfeng.chap5.selectkey;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

/**
 * 根据selectKey 找到对应的SelectionKey
 */
public class Test7 {
    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress("localhost",8888));
        serverSocketChannel.configureBlocking(false);
        // 获取选择器
        Selector selector = Selector.open();
        SelectionKey selectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        System.out.println(selectionKey+" "+selectionKey.hashCode());
        SelectionKey selectionKey1 = serverSocketChannel.keyFor(selector);

        System.out.println(selectionKey1+" "+selectionKey1.hashCode());
        serverSocketChannel.close();
    }
}
